package ThreadTest;

class SynThread02 implements Runnable{
    private int ticket=10;
    @Override
    public void run() {

        for (int i=0;i<50;i++){
            synchronized (this){
                if (this.ticket>0){
                    try {
                        Thread.sleep(3000);
                    }catch (Exception e){}
                    System.out.println(Thread.currentThread().getName()+"卖票,剩余:"+this.ticket--);
                }
            }
        }

    }
}

public class SynDemo02 {
    public static void main(String[] args) {
        SynThread02 syn02=new SynThread02();
        new Thread(syn02,"A：").start();
        new Thread(syn02,"B:").start();
        new Thread(syn02,"C").start();

    }
}
